package vn.nt.healthcare.server.ws.service.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.nt.healthcare.server.ws.domain.Company;
import vn.nt.healthcare.server.ws.model.TimeSpecial;
import vn.nt.healthcare.server.ws.repositories.CompanyRepository;
import vn.nt.healthcare.server.ws.service.TimeSpecialService;

@Service("timeSpecialService")
@Transactional
public class TimeSpecialServiceImpl implements TimeSpecialService {

	@Autowired
	private CompanyRepository companyRepository;

	@Override
	public TimeSpecial findSpecialTimeById(String companyId, int Id) {
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				TimeSpecial time = (TimeSpecial) session.get(TimeSpecial.class,
						Id);
				return time;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TimeSpecial> findTimes(String companyId) {
		// TODO Auto-generated method stub
		Company company = companyRepository.findByCode(companyId);
		if (company != null) {
			Session session = null;
			try {
				session = ServiceHelper.openSession(company);
				StringBuilder sql = new StringBuilder(
						"SELECT DISTINCT t FROM TimeSpecial t WHERE 1 = 1");
				Query query = session.createQuery(sql.toString());
				return query.list();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ServiceHelper.closeSession(session);
			}
		}
		return null;
	}

}
